import { Component, ViewChild } from "@angular/core";
import { IonicPage, NavController, NavParams, Navbar, Events } from "ionic-angular";

/**
 * Generated class for the PcTotalRulePage page.
 *
 * See https://ionicframework.com/docs/components/#navigation for more info on
 * Ionic pages and navigation.
 */

@IonicPage()
@Component({
  selector: "page-pc-total-rule",
  templateUrl: "pc-total-rule.html"
})
export class PcTotalRulePage {
  @ViewChild(Navbar) navBar: Navbar;
  allsort = 0;
  sortNumber = 0;
  alldiff = 0;
  diffNumber = 0;
  data = {
    allsort:null,
    alldiff:null
  }
  constructor(public navCtrl: NavController, public navParams: NavParams, private even: Events) {
    let numbers = this.navParams.get('numbers');
    let ruleNnmber = this.navParams.get('ruleNnmber');
    if(ruleNnmber){
      this.alldiff = ruleNnmber['alldiff'];
      this.allsort = ruleNnmber['allsort'];
    }
     
    //加小项分数
    for (let i = 0; i < numbers.length; i++) {
      this.diffNumber = this.diffNumber + numbers[i]['value'];
    }
  }

  ionViewDidLoad() {
    this.navBar.backButtonClick = this.backButtonClick;
  }
  backButtonClick = (e: UIEvent) => {
    this.returnCreater();
  };

  ionViewWillLeave() {
    this.even.unsubscribe('ruleNumber');
  }

  returnCreater() {
    this.data.allsort = this.allsort;
    this.data.alldiff = this.alldiff;
    this.even.publish('ruleNumber', this.data);
    this.navCtrl.pop();
  }
}
